package ui.panel
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	import globals.ButtonType;
	import globals.Global;

	import ui.common.EquipUpItem;
	import ui.component.Button;
	import ui.component.CheckBox;
	import ui.component.GameTextField;
	import ui.component.Label;
	import ui.component.ScaleBitmap;
	import ui.component.TextButton;
	import ui.component.TimerLabel;
	import ui.component.VBar;
	import ui.face.Bar;

	import util.DisplayUtil;
	import util.GameTween;
	import util.StringUtil;
	import util.TextStyle;
	import util.UIUtil;

	/**
	 * 强化装备
	 * @author luguobin
	 */
	public class EquipLevelUpPanel extends BasePanel
	{
		private var _upBtn:TextButton; //升级
		private var _downBtn:TextButton;
		private var _arrowLeft:Button;
		private var _arrowRight:Button;
		private var _listBg:ScaleBitmap;
		private var _vBar:VBar;
		private var _arrowBtm:Bitmap;
		private var _temperatureTxt:GameTextField; //温度计
		private var _itemList:Sprite;
		private var _items:Vector.<EquipUpItem>;
		private var _isSpeed:Boolean = false; //是加速还是升级
		private var _pointIndex:int = 0; //移动指针
		private var _curTotal:int = 0; //当前几个装备
		private var _isMouseDown:Boolean = false;
		private var _checkbox:CheckBox;
		private var _successTxt:GameTextField; //成功率
		private var _timerLabel:TimerLabel; //CD时间
		private var _returnCoin:GameTextField; //降级返还钱
		private var _goldLabel:Label;
		private var _silverLabel:Label; //银

		public function EquipLevelUpPanel()
		{
			this.x = 50;
			this.y = 15;
			super();
			pid = PanelType.EQUIP_LEVELUP_PANEL;
			isMask = true;
			updateTitle("强化");
		}

		override protected function updateBg():void
		{
			_bg.bitmapData = Global.assetsManager.getAssetsAsBitmapData("panel_bg_2");
		}

		override protected function initUI():void
		{
			_bg.x = 100;
			super.initUI();
			var contentBg:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("equip_levelUp_bg"));
			contentBg.x = _bg.x + (_bg.width - contentBg.width) * 0.5;
			contentBg.y = 33;
			this.addChild(contentBg);

			_vBar = new VBar();
			_vBar.x = _bg.x + 40;
			_vBar.y = _bg.y + 105;
			this.addChild(_vBar);
			_arrowBtm = new Bitmap();
			_arrowBtm.x = _bg.x + 50;
			_arrowBtm.y = _bg.y + 245;
			this.addChild(_arrowBtm);
			_temperatureTxt = new GameTextField();
			_temperatureTxt.defaultTextFormat = TextStyle.getTextFormat(0xffef02, 12, false, "center");
			_temperatureTxt.x = _bg.x + 25;
			_temperatureTxt.y = _bg.y + 65;
			this.addChild(_temperatureTxt);
			var _readme:GameTextField = new GameTextField();
			_readme.defaultTextFormat = TextStyle.getTextFormat(0xffef02, 12, false, "center");
			_readme.x = _bg.x + 25;
			_readme.y = _bg.y + 317;
			_readme.mytext = "每30分钟\n变化一次";
			this.addChild(_readme);
			_successTxt = new GameTextField();
			_successTxt.x = _bg.x + 200;
			_successTxt.y = _bg.y + 245;
			_successTxt.defaultTextFormat = TextStyle.getTextFormat(0xFFFFFF, 14, true);
			this.addChild(_successTxt);
			_checkbox = new CheckBox();
			_checkbox.x = _bg.x + 180;
			_checkbox.y = _bg.y + 270;
			_checkbox.htmlText = "<font color='#ffef02'>2金币进行高级强化</font>";
			this.addChild(_checkbox);

			_upBtn = UIUtil.getTextButton(0, "升级", ButtonType.BUTTON_GREEN_HEIGHT, btnHandler);
			_upBtn.x = _bg.x + 170;
			_upBtn.y = 370;
			this.addChild(_upBtn);
			_downBtn = UIUtil.getTextButton(1, "降级", ButtonType.BUTTON_BLUE_HEIGHT, btnHandler);
			_downBtn.x = _upBtn.x + 70;
			_downBtn.y = 370;
			this.addChild(_downBtn);
			_timerLabel = new TimerLabel(endUpTime);
			_timerLabel.defaultTextFormat = TextStyle.getTextFormat();
			_timerLabel.formatString = "HH:MM:SS";
			_timerLabel.x = _upBtn.x + 5;
			_timerLabel.y = 410;
			this.addChild(_timerLabel);
			_returnCoin = new GameTextField();
			_returnCoin.defaultTextFormat = TextStyle.getTextFormat();
			_returnCoin.x = _downBtn.x + 5;
			_returnCoin.y = 410;
			this.addChild(_returnCoin);

			var bitmap:Bitmap;
			bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("money_bg"));
			bitmap.x = _bg.x + 360;
			bitmap.y = 360;
			this.addChild(bitmap);
			bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("money_bg"));
			bitmap.x = _bg.x + 360;
			bitmap.y = 395;
			this.addChild(bitmap);
			bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("gold_icon"));
			bitmap.x = _bg.x + 340;
			bitmap.y = 350;
			this.addChild(bitmap);
			bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("silver_icon"));
			bitmap.x = _bg.x + 340;
			bitmap.y = 385;
			this.addChild(bitmap);
			_goldLabel = new Label(14, 0xFFFFFF, true, "left", true);
			_goldLabel.x = _bg.x + 375;
			_goldLabel.y = 363;
			this.addChild(_goldLabel);
			_silverLabel = new Label(14, 0xFFFFFF, true, "left", true);
			_silverLabel.x = _bg.x + 375;
			_silverLabel.y = 398;
			this.addChild(_silverLabel);

			////////下方列表的背景//////////////
			_listBg = new ScaleBitmap(Global.assetsManager.getAssetsAsBitmapData("float_content_bg"));
			_listBg.scale9Grid = new Rectangle(35, 35, 18, 18);
			_listBg.width = 640;
			_listBg.height = 120;
			_listBg.x = 10;
			_listBg.y = _bg.y + _bg.height - 10;
			this.addChild(_listBg);

			_arrowLeft = UIUtil.getButton(2, ButtonType.BUTTON_ARROW_2_LEFT, btnHandler);
			_arrowLeft.y = _listBg.y + (_listBg.height - _arrowLeft.height) * 0.5;
			_arrowLeft.onDownHandler = onDown;
			this.addChild(_arrowLeft);

			_arrowRight = UIUtil.getButton(3, ButtonType.BUTTON_ARROW_2_RIGHT, btnHandler);
			_arrowRight.x = 635;
			_arrowRight.y = _arrowLeft.y;
			_arrowRight.onDownHandler = onDown;
			this.addChild(_arrowRight);
			_itemList = new Sprite();
			_itemList.x = _listBg.x + 25;
			_itemList.y = _listBg.y + 22;
			this.addChild(_itemList);
			_items = new Vector.<EquipUpItem>;
			var listMask:Shape = new Shape();
			listMask.x = _itemList.x;
			listMask.y = _itemList.y;
			this.addChild(listMask);
			_itemList.mask = listMask;

			with(listMask.graphics)
			{
				beginFill(0x000000);
				drawRect(0, 0, 586, 76);
				endFill();
			}

			//TODO:测试
			update();
		}

		private function btnHandler(button:Button):void
		{
			_isMouseDown = false;

			switch(button.id)
			{
				case 0: //升级、加速
					break;
				case 1: //降级
					break;
				case 2: //左按
					break;
				case 3:
					break;
			}
		}

		private function onDown(button:Button, duration:Number = 0.5):void
		{
			if(button.id == 2) //左按钮
			{
				_pointIndex--;
				_arrowRight.enable = false;
			}
			else //右按钮
			{
				_pointIndex++;
				_arrowLeft.enable = false;
			}
			_isMouseDown = true;

			if(_pointIndex == 0)
			{
				_arrowLeft.enable = false;
				_isMouseDown = false;
			}
			else
			{
				_arrowLeft.enable = true;
			}

			if(_pointIndex == _curTotal - 7)
			{
				_arrowRight.enable = false;
				_isMouseDown = false;
			}
			else
			{
				_arrowRight.enable = true;
			}

			var newX:int = _listBg.x + 25 - (_pointIndex * 85);
			GameTween.toNew(_itemList, duration, {x: newX, onComplete: moveEnd, onCompleteParams: [button]});
		}

		private function moveEnd(button:Button):void
		{
			if(_isMouseDown)
			{
				onDown(button, 0.2);
			}
		}

		private function clickEquipItem(item:EquipUpItem):void
		{
			var len:int = _items.length;

			for(var i:int = 0; i < len; i++)
			{
				var curItem:EquipUpItem = _items[i];

				if(curItem.selected && curItem.id != item.id)
				{
					curItem.selected = false;
				}
			}
		}

		private function endUpTime():void
		{
			_timerLabel.clear();
			_upBtn.text = "升级";
			_isSpeed = false;
		}

		override public function clear():void
		{
			GameTween.remove(_arrowBtm);
		}

		public function update():void
		{
			_successTxt.mytext = StringUtil.substitute("成功率：{0}%", 40);
			_temperatureTxt.mytext = StringUtil.substitute("魔力炉温度\n{0}", 4000);

			if(true)
			{
				_timerLabel.leaveTime = 1000;
				_upBtn.text = "加速";
				_isSpeed = true;
			}
			else
			{
				endUpTime();
			}
			_returnCoin.mytext = StringUtil.substitute("银币+{0}", 300);

			if(true) //降
			{
				_vBar.update(40, 100, false);
				_arrowBtm.bitmapData = Global.assetsManager.getAssetsAsBitmapData("drop_arrow");
			}
			else
			{
				_vBar.update(40, 100, true);
				_arrowBtm.bitmapData = Global.assetsManager.getAssetsAsBitmapData("rise_arrow");
			}
//			GameTween.toNew(_arrowBtm, 10, {y: 0});
			GameTween.shock(_arrowBtm, 0);
			_goldLabel.text = "48000";
			_silverLabel.text = "48000";

			//////////////////
			_itemList.x = _listBg.x + 25;
			_arrowLeft.enable = false;
			DisplayUtil.removeAllChild(_itemList);
			_curTotal = 17;

			for(var i:int = 0; i < 17; i++)
			{
				if(_items.length == i)
				{
					var item:EquipUpItem = new EquipUpItem(i);
					item.x = i * 85;
					item.clickCallback = clickEquipItem;
					_items.push(item);
				}
				var curItem:EquipUpItem = _items[i];
				curItem.update();
				_itemList.addChild(curItem);
			}

			if(_itemList.numChildren <= 7)
			{
				_arrowRight.enable = false;
			}
		}
	}
}